﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WebSiteAdminTool.UI.Utils.Commands;
using System.Linq;
using WebSiteAdminTool.UI.Views.Security;
using WebSiteAdminTool.UI.Services;
using System.ComponentModel;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight;
using WebSiteAdminTool.UI.Services.ModalDialogs;
using WebSiteAdminTool.UI.Services.Messaging;
using WebSiteAdminTool.UI.Services.Messaging.DataManipulationMessages;
using WebSiteAdminTool.UI.Utils;

namespace WebSiteAdminTool.UI.ViewModels.Base
{
    /// <summary>
    /// The purpose of this viewmodel class is to allow modal window closing from VM code
    /// not the best approach but works so far
    /// </summary>
    public abstract class BaseDialogViewModel : BaseViewModel
    {
        #region private data

        private DialogMode _mode = DialogMode.New;
        private IModalWindow _dialog = null;
        // commands
        private GenericCommand _saveCommand;
        private GenericCommand _cancelEditCommand;

        #endregion

        #region ctors

        public BaseDialogViewModel()
        {
            _saveCommand = new GenericCommand(this.SaveData, true);
            _cancelEditCommand = new GenericCommand(this.CloseDialog, true);
            
            base.InitViewModel();
        }

        #endregion

        #region public props

        public GenericCommand SaveCommand { get { return _saveCommand; } }
        public GenericCommand CancelEditCommand { get { return _cancelEditCommand; } }

        public DialogMode Mode
        {
            get { return _mode; }
            set { _mode = value; }
        }

        public IModalWindow Dialog 
        {
            set { _dialog = value; } 
        }

        public virtual string DialogTitle
        {
            get { return "Title not set"; }
        }

        #endregion

        #region helpers

        protected virtual bool CanSave
        {
            get { return _saveCommand.IsEnabled; }
            set { _saveCommand.IsEnabled = value; }
        }

        private void SaveData(object data)
        {
            DoSave();
        }

        private void CloseDialog(object data = null)
        {
            this._dialog.Close();
        }

        protected virtual void DoSave()
        {
            throw new NotImplementedException();
        }

        protected void SaveCompleted<T>(AsyncCompletedEventArgs e, T savedEntity)
        {
            if (e.IsServiceCallSuccessfull() && null != savedEntity)
            {
                if (this.Mode == DialogMode.New)
                    MessageManager.SendMessage<DataObjectCreated<T>>(new DataObjectCreated<T>(savedEntity));
                if (this.Mode == DialogMode.Edit)
                    MessageManager.SendMessage<DataObjectUpdated<T>>(new DataObjectUpdated<T>(savedEntity));

                CloseDialog();
            }
        }

        #endregion
    }

    public enum DialogMode
    {
        New,
        Edit
    }
}
